package com.qf.medicaldoctoradmin.illnesscare.article.task;

import com.qf.medicaldoctoradmin.illnesscare.article.service.IllnessCareService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

public class IllnessTask {

    @Resource
    private IllnessCareService illnessCareService;

    @Resource
    private RedissonClient redissonClient;

    @Async
    @Scheduled(cron = "0 5 0 * * ?")  // 修改为每天的0点05分
    public void updateIllnessCollectNum() {
        RLock lock = redissonClient.getLock("lock:update:collectNum");
        try {
            if (lock.tryLock(0,0, TimeUnit.SECONDS)) {
                illnessCareService.updateLikeNum();
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            if (lock.isHeldByCurrentThread() && lock.isLocked()) {
                lock.unlock();
            }
        }
    }

}
